<?php

namespace App\Console;

use App\Console\Commands\CalculateLiquidatedTaskCommand;
use App\Console\Commands\SendTemplateTaskCommand;
use App\Console\Commands\TaskCommand;
use App\Http\Common\Globals;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use Illuminate\Support\Facades\DB;

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     * 阿是大师大师的
     * @var array
     */
    protected $commands = [
        TaskCommand::class,
    ];

    /**
     * Define the application's command schedule.
     * @param Schedule $schedule
     奥术大师大所大所多
     * @return void
     * ->withoutOverlapping() 为防止其他的运行了，多线程
     */
    protected function schedule(Schedule $schedule)
    {
        // 禁用掉日志
        DB::unsetEventDispatcher();
        Globals::$writeLog = false;
        //调用artisan
        $time1 = getConfig('BILL_CYCLE_TYPE_TIME');
        $billCycleType = getConfig('BILL_CYCLE_TYPE');
        foreach ($this->commands as $k=>$v){
            switch ($billCycleType){
                case "月":
                    $schedule->command($v)->monthlyOn($time1)->withoutOverlapping();
                    break;
                case "分钟":
                    $schedule->command($v)->everyMinute()->withoutOverlapping();
                    break;
                case "天":
                    $schedule->command($v)->dailyAt($time1)->withoutOverlapping();
                    break;
                case "周":
                    $schedule->command($v)->weeklyOn($time1)->withoutOverlapping();
                    break;
                case "小时":
                    $schedule->command($v)->hourlyAt($time1)->withoutOverlapping();
                    break;
                case "年":
                    $schedule->command($v)->yearly()->withoutOverlapping();
                    break;
            }
        }
        //调用 定时清除redis   每 REDIS_FLUSH_CYCLE_TYPE  的   REDIS_FLUSH_CYCLE_TIME  时间点
        $redisFlushCycleTime = getConfig('REDIS_FLUSH_CYCLE_TIME');
        $redisFlushCycleType = getConfig('REDIS_FLUSH_CYCLE_TYPE');
        switch ($redisFlushCycleType){
            case "月":
                $schedule->command('task:flushRedisTask')->monthlyOn($redisFlushCycleTime)->withoutOverlapping();
                break;
            case "分钟":
                $schedule->command('task:flushRedisTask')->everyMinute()->withoutOverlapping();
                break;
            case "天":
                $schedule->command('task:flushRedisTask')->dailyAt($redisFlushCycleTime)->withoutOverlapping();
                break;
            case "周":
                $schedule->command('task:flushRedisTask')->weeklyOn($redisFlushCycleTime)->withoutOverlapping();
                break;
            case "小时":
                $schedule->command('task:flushRedisTask')->hourlyAt($redisFlushCycleTime)->withoutOverlapping();
                break;
            case "年":
                $schedule->command('task:flushRedisTask')->yearly()->withoutOverlapping();
                break;
        }
    }

    /**
     * 本地修改数据
     */
    public function getAllTask(){

    }

    /**
     * Register the commands for the application.
     * @return void
     */
    protected function commands()
    {
        $this->load(__DIR__.'/Commands');

        require base_path('routes/console.php');
    }
}
